<?php 
	/**
	* FILE: ProcessAuthToRelease.php
	* PATH: /web_root/models/
	* NOTES:
	*	works for VAIL demo 2008-10-23
	*/
	session_start();
	/**
	* initialize includes
	*/
	include('../includes/db_connect.php'); 
	//include('../includes/phpheader.php');
	/**
	* form id
	*/
	$id = $_POST["id"];
	/**
	* loggedin id
	*/
	$userId = $_SESSION['user_id'];
	//echo "<h1>".$userId."</h1>";
	
	/**
	* list of agency options -- should be in a loop
	*/
	$agency1 = $_POST["agency1"];
	$agency2 = $_POST["agency2"];
	$agency3 = $_POST["agency3"];
	$agency4 = $_POST["agency4"];
	$agency5 = $_POST["agency5"];
	$person1 = $_POST["person1"];
	$person2 = $_POST["person2"];
	$person3 = $_POST["person3"];
	$person4 = $_POST["person4"];
	$person5 = $_POST["person5"];
	/**
	*consumer form data
	*/
	$consumerSig 				= $_POST["consumerSignature"];
	$consumerSigDateMonth 	= $_POST["consumerSigDateMonth"];
	$consumerSigDateDay 		= $_POST["consumerSigDateDay"];
	$consumerSigDateYear 	= $_POST["consumerSigDateYear"];
	$consumerSigDate 			= $consumerSigDateYear."-".$consumerSigDateMonth."-".$consumerSigDateDay;
	/**
	*guardian for data
	*/
	$guardianSig 				= $_POST["guardianSignature"];
	$guardianSigDateMonth 	= $_POST["guardianSigDateMonth"];
	$guardianSigDateDay 		= $_POST["guardianSigDateDay"];
	$guardianSigDateYear 	= $_POST["guardianSigDateYear"];
	$guardianSigDate 			= $guardianSigDateYear."-".$guardianSigDateMonth."-".$guardianSigDateDay;
	/**
	*guardian for data
	*/
	$witnessSig 				= $_POST["witnessSignature"];
	//echo "<h1>".$witnessSig."</h1>";
	/**
	*coordinator sig
	*/
	$employeeSig = $_POST["employeeSignature"];	
	/**
	* find the current ATR instance in the db
	*/
	$query = "SELECT id 
			     FROM authToRelease 
			    WHERE consumer_id = ".$id;
				
	if(mysql_fetch_array(mysql_query($query))) {
		/**
		* we found the record, so lets just update it
		*/
		$query = "UPDATE authToRelease 
				    SET agency1 = '".$agency1."', agency2 = '".$agency2."', agency3 = '".$agency3."', 
					     agency4 = '".$agency4."', agency5 = '".$agency5."', person1 = '".$person1."', 
					     person2 = '".$person2."', person3 = '".$person3."', person4 = '".$person4."', 
					    person5 = '".$person5."', consumer_signature = '".$consumerSig."',
					   consumer_signature_date = '".$consumerSigDate."', guardian_signature = '".$guardianSig."', 
                                           guardian_signature_date = '".$guardianSigDate."', witness_signature = '".$witnessSig."',
					   employee_signature = '".$employeeSig."' where consumer_id = ".$id;
	} else {
		/**
		*this is a new ATR record, so let's create it
		*/
		$query = "INSERT INTO authToRelease (consumer_id, 
													agency1, 
													agency2, 
													agency3, 
													agency4, 
													agency5, 
													person1, 
													person2, 
													person3,
													person4, 
													person5, 
													consumer_signature, 
													consumer_signature_date, 
													guardian_signature, 
													guardian_signature_date, 
													witness_signature,
													employee_signature, 
													entered_by) 
					VALUES (".$id.", 
							   '".$agency1."', 
							   '".$agency2."', 
							   '".$agency3."', 
							   '".$agency4."', 
							   '".$agency5."', 
							   '".$person1."', 
							   '".$person2."', 
							   '".$person3."', 
							   '".$person4."', 
							   '".$person5."', 
							   '".$consumerSig."', 
							   '".$consumerSigDate."', 
							   '".$guardianSig."', 
							   '".$guardianSigDate."', 
							   '".$witnessSig."',
							   '".$employeeSig."', 
							   ".$userId.");";
	}

	if(mysql_query($query)) {
		//echo "Success <br/>".$query;
		header("Location:../admin.php?action=cieedit&id=".$id."&Status=Authorization To Release Form Submitted Successfully");
		exit();
		//echo "Success <br/>".$query;
	} else {
		//echo "Failure <br/>".$query.mysql_error();
		header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process Authorization To Release Form due to system error.  Please contact system administrator.");
		exit();
		//echo "Failure <br/>".$query;
	}
?>